<template>
  <div>
    <el-button type="warning" style="margin-bottom: 10px" @click="addCategory"
      >新增分类</el-button
    >

    <el-dialog
      v-model="dialogVisible"
      :title="currentCategory.id ? '编辑分类' : '新增分类'"
      width="30%"
    >
      <el-form :model="currentCategory" label-width="80px">
        <el-form-item label="分类名称">
          <el-input v-model="currentCategory.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input-number
            v-model="currentCategory.sort"
            :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="状态">
          <el-switch
            v-model="currentCategory.status"
            :active-value="1"
            :inactive-value="0"
          ></el-switch>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveCategory">保存</el-button>
      </template>
    </el-dialog>

    <el-table :data="categories" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="name" label="分类名称"></el-table-column>
      <el-table-column prop="sort" label="排序" width="100"></el-table-column>
      <el-table-column label="状态" width="100">
        <template #default="{ row }">
          <el-tag :type="row.status === 1 ? 'success' : 'danger'">
            {{ row.status === 1 ? "启用" : "禁用" }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button size="small" @click="editCategory(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="deleteCategory(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";

const categories = ref([]);
const dialogVisible = ref(false);
const currentCategory = ref({
  id: null,
  name: "",
  sort: 0,
  status: 1,
});

// 模拟获取分类数据
const fetchCategories = async () => {
  categories.value = [
    { id: 1, name: "狗粮", sort: 1, status: 1 },
    { id: 2, name: "猫用品", sort: 2, status: 1 },
    { id: 3, name: "玩具", sort: 3, status: 1 },
    { id: 4, name: "保健品", sort: 4, status: 0 },
  ];
};

const addCategory = () => {
  currentCategory.value = {
    id: null,
    name: "",
    sort: 0,
    status: 1,
  };
  dialogVisible.value = true;
};

const editCategory = (category) => {
  currentCategory.value = { ...category };
  dialogVisible.value = true;
};

const saveCategory = () => {
  if (!currentCategory.value.name) {
    ElMessage.error("请输入分类名称");
    return;
  }

  if (currentCategory.value.id) {
    // 更新分类
    const index = categories.value.findIndex(
      (c) => c.id === currentCategory.value.id
    );
    if (index !== -1) {
      categories.value[index] = { ...currentCategory.value };
    }
    ElMessage.success("分类更新成功");
  } else {
    // 新增分类
    currentCategory.value.id = categories.value.length + 1;
    categories.value.unshift({ ...currentCategory.value });
    ElMessage.success("分类添加成功");
  }

  dialogVisible.value = false;
};

const deleteCategory = (category) => {
  ElMessageBox.confirm("确定删除此分类吗?", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      categories.value = categories.value.filter((c) => c.id !== category.id);
      ElMessage.success("删除成功");
    })
    .catch(() => {});
};

onMounted(() => {
  fetchCategories();
});
</script>
